package com.tzbank.face.sys;

import java.util.List;

import com.tzbank.face.entity.Config;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import com.tzbank.face.dao.UtilsMapper;
import com.tzbank.redis.RedisUtils;
import com.tzbank.utils.SpringContextUtils;

/**
 * 启动初始化类
 *
 */
@Component
public class ApplicationRunner implements CommandLineRunner,ApplicationContextAware {

	@Autowired
	private UtilsMapper utilsMapper;
	@Override
	public void run(String... args) throws Exception {
		init();
	}

	/**
	 * 配置信息出初始化 放入redis 缓存
	 */
	private void init() {
		List<Config> FaceConfig = utilsMapper.selectConfig();
		for (Config configvalue : FaceConfig) {
			if ("ACCESS_KEY".equalsIgnoreCase(configvalue.getConfigName())) {
				RedisUtils.setKey("FACE_ACCESS_KEY", configvalue.getConfigValue());
			}
			if ("ACCESS_SECRET".equalsIgnoreCase(configvalue.getConfigName())) {
				RedisUtils.setKey("FACE_ACCESS_SECRET", configvalue.getConfigValue());
			}
			if ("ENDPOINT".equalsIgnoreCase(configvalue.getConfigName())) {
				RedisUtils.setKey("FACE_ENDPOINT", configvalue.getConfigValue());
			}
			if ("PRODUCT_INSTANCE_ID".equalsIgnoreCase(configvalue.getConfigName())) {
				RedisUtils.setKey("FACE_PRODUCT_INSTANCE_ID", configvalue.getConfigValue());
			}
		}
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		SpringContextUtils.setApplicationContext(applicationContext);
	}
}
